#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
class Solution
{
public:
    int minimumEffort(vector<vector<int>> &tasks)
    {
        vector<tuple<int, int, int>> v;
        for (int i = 0; i < tasks.size(); i++)
        {
            v.push_back(make_tuple(tasks[i][0], tasks[i][1], tasks[i][1] - tasks[i][0]));
        }
        sort(v.begin(), v.end(), [](tuple<int, int, int> &a, tuple<int, int, int> &b)
             {    
            if (get<2>(a) != get<2>(b)) return get<2>(a) < get<2>(b);
            if (get<1>(a) != get<1>(b)) return get<1>(a) < get<1>(b);
            return get<0>(a) < get<0>(b); });
        int ans = get<1>(v[0]);
        for (int i = 1; i < tasks.size(); i++)
        {
            ans += get<0>(v[i]);
            ans = max(ans, get<1>(v[i]));
        }
        return ans;
    }
};
int main()
{
    cout << "Code1665" << endl;
    Solution s;
    vector<vector<int>> tasks = {{3, 3}, {1, 6}, {1, 10}, {1, 2}, {4, 7}, {2, 4}};
    cout << s.minimumEffort(tasks) << endl;
    return 0;
}